package client

import (
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
	"grpc-gateway-demo/proto/pb"
	"log"
)

var (
	Port        string
	CertPemPath string
	CertName    string
)

func Client() error {
	cred, err := credentials.NewClientTLSFromFile(CertPemPath, CertName)
	if err != nil {
		log.Printf("Failed to create TLS credentials %v\n", err)
		return err
	}
	endPoint := ":" + Port
	conn, err := grpc.Dial(endPoint, grpc.WithTransportCredentials(cred))
	defer conn.Close()

	if err != nil {
		log.Println(err)
		return err
	}

	c := pb.NewHelloWorldClient(conn)
	ctx := context.Background()
	body := &pb.HelloWorldRequest{
		Name: "CXT",
	}
	r, err := c.SayHelloWorld(ctx, body)
	if err != nil {
		log.Println(err)
		return err
	}
	log.Println(r.Message)
	return nil
}
